/*
 * Copyright (c) 2020, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _DEBUGH_
#define _DEBUGH_

#include "atom.h"
#include "iosdrv.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifdef COM_USE_DBG_CMD
	void DebugCmdInit(void);
#else
    #define DebugCmdInit()
#endif

/*
 * The color for terminal (foreground)
 * BLACK    30
 * RED      31
 * GREEN    32
 * YELLOW   33
 * BLUE     34
 * PURPLE   35
 * CYAN     36
 * WHITE    37
 */
#ifdef COM_USE_DBGPORT
	void KPrint(const char* str);
    // @note 使用KPrintf函数输出字符串需要多占用COM_MYPRINTF_BUFFER字节的堆栈，请注意！！
    void KPrintf(const char *format, ... );
    void KDumpPrint(uint8_t *addr, uint32_t len);
	void KPrintValue(const char* prompt, uint32_t value, unsigned int radix, int prefix, int newline);

    void    DebugPortSetDevice(const char *name);
    FD      DebugPortGetDevice(void);
    void    DebugPortPutChar(char c);
    int     DebugPortIsReadable(void);
    uint8_t DebugPortGetChar(void);
    void    DebugPortPutStr(const char *str);
#else
    #define KPrint(str)
    #define KPrintf(format, ...)
    #define KDumpPrint(addr, len)
    #define KPrintValue(prompt, value, radix, prefix, newline)

    #define DebugPortPutChar(c)
    #define DebugPortPutStr(const char *str)
    #define DebugPortIsReadable() 0
    #define DebugPortGetChar(void) 0
    #define DebugPortSetDevice(name)
    #define DebugPortGetDevice() ERR_FD
#endif    /* COM_USE_DBGPORT */


#if defined(COM_USE_DBG_COLOR) && defined(COM_USE_DBGPORT)
#define DBG_COLOR(n)         KPrint("\033["#n"m")
#define DBG_COLOR_END()      KPrint("\033[0m")
#else
#define DBG_COLOR(n)
#define DBG_COLOR_END()     KPrint("\r\n")
#endif /* DBG_COLOR */

#define DBG_COLOR_GREEN()  DBG_COLOR(32)
#define DBG_COLOR_RED()    DBG_COLOR(31)

#ifdef __cplusplus
}
#endif

#endif